function valoration(tweet, like_value){
    $.ajax({
        type: "POST",
        url: "http://nibble.li/twittertido/web/like.php",
        data: ({
            tweet_id: tweet, 
            like: like_value
        })
    });
}

function votarPositivo(tweet){
    like(tweet);
    tweet_row= '#tweet_'+tweet;
    $(tweet_row).hide();
}

function votarNegativo(tweet){
    dislike(tweet);
    tweet_row= '#tweet_'+tweet;
    $(tweet_row).hide();
}
            
function like(tweet){
    valoration(tweet, 1);
    ic_like = "#ic_like_"+tweet;
    ic_dislike = "#ic_dislike_"+tweet;
    $(ic_like).attr("src", "img/ic_like_green");
    $(ic_dislike).hide();
}
            
function dislike(tweet){
    valoration(tweet, -1);
    ic_like = "#ic_like_"+tweet;
    ic_dislike = "#ic_dislike_"+tweet;
    $(ic_dislike).attr("src", "img/ic_dislike_red");
    $(ic_like).hide();
}

function login(){
    $('#login').html("Comprobando");
    if($('#checkbox').is(':checked')){
        remember_chk = "y";
    }else{
        remember_chk = "n";
    }
    $.ajax({
        url: 'ajaxLogin.php',
        data: {
            username : $('#user').val(),
            password: $('#password').val(),
            remember: remember_chk
        },
        success: function(data) {
            if(data==1){
                $('#login').html("Validado");
                location.reload();
            }else{
                $('#login').html("Entrar");
                $('#error_login').show();
            }
        }
    });   
}

$(function() {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function(login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.mouseup(function() { 
        return false;
    });
    $(this).mouseup(function(login) {
        if(!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });
});

function comment(){
    $('#button_comment').html('Enviando');
    $('#button_comment').css({ opacity: 0.5 });
    $.ajax({
        url: 'ajaxComment.php',
        data: {
            comment : encodeURIComponent($('#comment').val()),
            tweet_id: $('#tweet_id').val(),
            user: $('#user').val()
        },
        success: function(data) {
            if(data>0){
                $('#button_comment').html('Enviado');
                location.reload();
            }else{
                $('#error_comment').show();
                $('#button_comment').html('Enviar');
                $('#button_comment').css({ opacity: 1 });
            }
        }
    });   
}     